package info4302.books.service;

import info4302.books.sparql.dbpedia.DBPediaSPARQLRunner;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;

/**
 * Servlet implementation class BooksRestfulWS
 */
public class BooksRestfulWS extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Document data;
	List<String> pairs ;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BooksRestfulWS() {
		super();
	}

	@Override
	public void init() throws ServletException {
		super.init();

		// read the data at startup
		pairs = handleDBPediaMovieBook();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		out.print("<html>");
		out.print("<body>");
		out.print("<h>Movies and Books<h>");
		out.print("<ul>");
		for (String pair : pairs) {
			out.print("<li>");
			out.print(pair);
			out.print("</li>");
		}

		out.print("</ul>");
		out.print("</body>");
		out.print("</html>");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	private List<String> handleDBPediaMovieBook() {
		return DBPediaSPARQLRunner.getMovieBasedOnBookPairs();
	}

}
